#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
管理员界面
"""
import sys
import pickle
from lib.hash import hash_mad5
from lib.log import logger


def creation_course():
    """
    创建课程
    :return:
    """
    print('##########创建课程##########')
    while True:
        course_name = input('请输入课程名称(N返回)：')
        if course_name.upper() == 'N':
            return
        course_price = input('请输入课程价格：')
        course_period = input('请输入课程周期：')
        course_teacher = input('请输入课程老师：')
        num = None
        with open(r'../db/course.txt', 'r', encoding='utf-8') as f:
            for line in f:
                num = line.strip().split('|')[0]
        with open(r'../db/course.txt', 'a', encoding='utf-8') as f:
            f.write('%s|%s|%s|%s|%s\n' % ((int(num) + 1), course_name, course_price, course_period, course_teacher))
            print('创建课程成功')
            logger.info('创建了%s课程' % course_name)


def add_user():
    """
    注册用户
    :return:
    """
    print('##########创建用户##########')
    while True:
        user = input('请输入用户名(N返回)：')
        if user.upper() == 'N':
            return
        pwd = input('请输入密码：')
        admin = input(
            '管理员：A\n'
            '学生：S\n'
            '请输入：'
        )
        if admin.upper() == 'A':
            identity = 'administrator'
        elif admin.upper() == 'S':
            identity = 'student'
        else:
            print('请输入')
        with open(r'../db/user.txt', 'a', encoding='utf-8') as f:
            f.write('%s|%s|%s\n' % (user, hash_mad5(pwd), identity))
            logger.info('创建了用户%s' % user)


def check_all_courses():
    """
    查看所有课程
    :return:
    """
    with open('../db/course.txt', 'r', encoding='utf-8') as f:
        key = f.readline().strip()
        print(key)
        for line in f:
            value = line.strip()
            print(value)
        return


def check_all_students():
    """
    查看所有学生
    :return:
    """
    with open('../db/user.txt', 'r', encoding='utf-8') as f:
        key = f.readline().strip().split('|')
        values = list()
        print(key[0])
        for line in f:
            value = line.strip().split('|')
            if value[2] == 'administrator':
                continue
            else:
                values.append(value[0])
        return values


def check_all_elective():
    """
    查看所有学生的选课情况
    :return:
    """
    with open(r'../db/user_course.txt', 'rb') as f:
        data = pickle.load(f)
        for i in check_all_students():
            print(i)
            for j in data[i]:
                print(j)


def admin_page(name):
    print('欢迎管理员 %s登录' % name)
    while True:
        print('##########管理员用户##########\n'
              '1.创建课程\n'
              '2.创建用户\n'
              '3.查看所有课程\n'
              '4.查看所有学生\n'
              '5.查看所有学生的选课情况\n'
              '6.退出程序')
        page_list = {'1': creation_course,
                     '2': add_user,
                     '3': check_all_courses,
                     '5': check_all_elective}
        num = input('请输入序号选择:')
        if num == '6':
            sys.exit('谢谢使用！')
        if num == '4':
            for i in check_all_students():
                print(i)
        if page_list.get(num):
            page_list.get(num)()
        elif num == '4':
            pass
        else:
            print('请输入正确的序号！')
            continue
